#include "view_messages.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "font_mngr.h"
#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_message_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
    }
    int x = 74;
    int y = 36;
    img_gif_com_t const img_cfg = {
        .gif_count = 3,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_X101_message_icon_1_74_36,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_X102_message_icon_2_74_36,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_X103_message_icon_3_74_36,

    };
    _disp_show_flash_img_gif_all(&img_cfg, DISP_LFS_FILE_INDEX_0);

    // x = 79;
    // y = 155;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_X104_message_text_79_155);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Message, 50);
}
#define TEXT_X1 36
#define TEXT_Y1 86
#define TEXT_X2 (TEXT_X1 + 168)
#define TEXT_Y2 (TEXT_Y1 + 120)
void view_message_page_display(void)
{
    uint16_t *unicode_str = (uint16_t *)(&disp_ctrl.com_data.i_temp[4]);
    if (disp_ctrl.loop_cnt == 0) //更新数据需求
    {
        disp_ctrl.auto_refresh_time = 1000;
        if (ancs_msg_get_count() == 0)
        {
            disp_ctrl.com_data.int16_temp[5] = 0;
            if (disp_ctrl.auto_refresh_count >= 3)
            {
                display_show(view_messages, DISP_MODE_NORMAL);
                return;
            }
        }
        else
        {
            disp_ctrl.com_data.int16_temp[5] = 1;
            if (ancs_disp_ctrl.new_flag)
            {
                ancs_message_package_t msg = {0};
                ancs_msg_get(&msg, ancs_disp_ctrl.read_num);
                ancs_disp_ctrl.message_type = msg.ancs_msgType;
                // LOG("get_msg[%d][%s]", ancs_disp_ctrl.read_num, msg.ancs_msg.appAttrMessage);
                utf8_string_to_unicode_string((uint8_t *)msg.ancs_msg.appAttrMessage, (uint8_t *)unicode_str,
                                              ((uint8_t *)(disp_ctrl.com_data.i_temp)) + sizeof(disp_ctrl.com_data.i_temp) - (uint8_t *)unicode_str);
                disp_ctrl.com_data.int16_temp[4] = arabic_reshaper(unicode_str);
                ancs_disp_ctrl.message_len = wcslen(unicode_str) * 2;
                ancs_disp_ctrl.new_flag = 0;
                ancs_disp_ctrl.disp_offset = 0;
            }
            disp_ctrl.com_data.int16_temp[0] = ancs_disp_ctrl.disp_offset;
            disp_ctrl.com_data.int16_temp[1] = 0;
            disp_ctrl.com_data.int16_temp[2] = 0;
            ancs_disp_ctrl.next_offset = disp_show_font_string_page_cal(TEXT_X1, TEXT_Y1, TEXT_X2, TEXT_Y2, unicode_str, 16) & 0xffff;
            // LOG("msg_offset[%d]", disp_ctrl.com_data.int16_temp[0]);
        }
    }
    if (!disp_ctrl.com_data.int16_temp[5])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 96, 63, FILE_IMG_X108_message_icon_96_63);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 53, 151, FILE_IMG_D116_nomessage_53_151);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_NoMessageAlert, 50);
        return;
    }
    const uint8_t message_img_tab[] = {FILE_IMG_D115_other_100_11,     FILE_IMG_D118_message_100_11,  FILE_IMG_D106_weixin_100_11,    FILE_IMG_D105_qq_100_11,
                                       FILE_IMG_D105_qq_100_11,        FILE_IMG_D107_facebook_100_11, FILE_IMG_D112_messenger_100_11, FILE_IMG_D109_whatsapp_100_11,
                                       FILE_IMG_D108_twitter_100_11,   FILE_IMG_D113_skype_100_11,    FILE_IMG_D110_line_100_11,      FILE_IMG_D115_other_100_11,
                                       FILE_IMG_D111_instagram_100_11, FILE_IMG_D120_tim_100_11,      FILE_IMG_D119_snapchat_100_11,  FILE_IMG_D115_other_100_11};

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 100, 25, message_img_tab[ancs_disp_ctrl.message_type % sizeof(message_img_tab)]);

    // LOG("x1[%d] y1[%d] offset[%d] offset_str[%d]", TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], disp_ctrl.com_data.int16_temp[0], disp_ctrl.com_data.int16_temp[2]);
    uint32_t temp16 = disp_show_font_string(TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], TEXT_X2, TEXT_Y2,
                                            (uint16_t *)((uint8_t *)unicode_str + disp_ctrl.com_data.int16_temp[0] + disp_ctrl.com_data.int16_temp[2]), 16, &disp_ctrl.com_data.int16_temp[4]);
    if (temp16)
    {
        disp_ctrl.com_data.int16_temp[2] += (temp16 & 0xffff);
        disp_ctrl.com_data.int16_temp[1] += ((temp16 >> 16) * (FONT_SIZE + FONT_SIZE_VERTICAL_INTERVAL));
        // LOG("offset_str[%d] offset_y[%d]", disp_ctrl.com_data.int16_temp[1], disp_ctrl.com_data.int16_temp[2]);
    }
    if (disp_ctrl.com_data.int16_temp[0] != 0) //非首页的继续显示
    {
        if ((disp_ctrl.com_data.int16_temp[0] + ancs_disp_ctrl.next_offset) < ancs_disp_ctrl.message_len) //非最后一页
        {
            disp_draw_user_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
    else
    {
        if (ancs_disp_ctrl.next_offset < ancs_disp_ctrl.message_len) //多页
        {
            disp_draw_user_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
}
view_event_result_t view_message_page_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        if (param->previous_view != view_message_page)
        {
            ancs_disp_ctrl.new_flag = 1;
            ancs_disp_ctrl.read_num = 0;
            return view_event_result_handled;
        }
        break;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_messages, DISP_MODE_NORMAL);
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        if (ancs_msg_get_count() == 0)
        {
            display_show(view_time, DISP_MODE_NORMAL);
        }
        else
        {
            ancs_disp_ctrl.disp_offset = 0;
            ancs_disp_ctrl.new_flag = 1;
            if (ancs_disp_ctrl.read_num == 0)
            {
            }
            else
            {
                ancs_disp_ctrl.read_num--;
            }
            display_show(view_message_page, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    {
#endif
        if (ancs_msg_get_count() == 0)
        {
            display_show(view_messages, DISP_MODE_NORMAL);
        }
        else
        {
            int new_offset = ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset; //偏移计算
            if (new_offset >= ancs_disp_ctrl.message_len)
            {
                if (ancs_disp_ctrl.read_num + 1 >= ancs_msg_get_count())
                {
#ifdef EVENT_NEXT_VIEW
#else
                    ancs_disp_ctrl.read_num = 0;
                    ancs_disp_ctrl.disp_offset = 0;
                    ancs_disp_ctrl.new_flag = 1;
                    display_show(view_message_page, DISP_MODE_DOWN_MOVE);
#endif
                }
                else
                {
                    ancs_disp_ctrl.read_num++;
                    ancs_disp_ctrl.disp_offset = 0;
                    ancs_disp_ctrl.new_flag = 1;
                    display_show(view_message_page, DISP_MODE_DOWN_MOVE);
                }
            }
            else
            {
                ancs_disp_ctrl.disp_offset = new_offset;
                display_show(view_message_page, DISP_MODE_DOWN_MOVE);
            }
        }
        return view_event_result_handled;
    }

    default:
        break;
    }
    return view_event_result_not_handled;
}
void view_message_display(int view)
{
    if (view == view_message_base)
    {
        view_message_base_display();
    }
    else
    {
        view_message_page_display();
    }
}
view_event_result_t view_message_event(int view, event_param_t const *param)
{
    if (view == view_message_base)
    {
        return view_event_result_not_handled;
    }
    else
    {
        return view_message_page_event(param);
    }
}
